package demo1.web.servlet.userBuyapi;

import demo1.dao.Mapper.userBuyMapper;
import demo1.pojo.gouwucar;
import demo1.pojo.orderform;
import demo1.pojo.userBuy;
import demo1.dao.sql.userBuysql;
import demo1.service.userBuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/createOrderFormByGroup")
public class createOrderFormByGroup extends HttpServlet {//生成购物车中部分商品的订单
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        int id = Integer.parseInt(req.getParameter("id"));
        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        userBuy users = (userBuy) userObject;

        try {
            userBuyService create = new userBuyService();
            create.checkGoodsStatue(users.getGouwuid());//检查商品状态
            int num = create.checkOrderForm(users.getGouwuid());//先检查有没有订单
            if(num == 1){
                //如果存在订单
                resp.sendRedirect("failOrderForm.jsp");
            }
            else {
                orderform orderform = create.createOrderFormByGroup(users.getGouwuid(), id);
                create.decrease(users.getGouwuid());//增加销量，减少库存
                req.setAttribute("orderform",orderform);//把数据存储起来
                req.getRequestDispatcher("showOrderForm.jsp").forward(req,resp);//转发到另一个界面
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
